﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SGArchiveSystem7xDB.Linq
{
    public class T_LOGVIEWDal
    {
        static SGArchiveSystem7xEntities conn;
        public static List<T_LOGVIEW> Get_LogView(string name,string dh,string  starttime,string endtime, out int count, int start = -1, int row = -1)
        {
            count = 0;
            try
            {
                conn = new SGArchiveSystem7xEntities();
                var query = conn.T_LOGVIEW.Select(c => c);
                if (name != null && name != "")
                    query = query.Where(c => c.LogView_USERNAME.Contains(name));
                if (dh != null && dh != "")
                    query = query.Where(c => c.LogView_DH.Contains(dh));
                if (starttime != null && starttime != "")
                {
                    DateTime st = DateTime.Parse(starttime);
                    query = query.Where(c => c.LogView_DateTime >= st);
                }
                if (endtime != null && endtime != "")
                {
                    DateTime et = DateTime.Parse(endtime);
                    query = query.Where(c => c.LogView_DateTime <= et);
                }
                if (start != -1 && row != -1)
                {
                    count = query.Count();
                    return query.OrderBy(c => c.LogView_ID).Skip(start).Take(row).ToList();
                }
                else
                {
                    var list = query.ToList();
                    count = list.Count;
                    return list;
                }
            }
            catch (Exception ex)
            { return null; }
        }
        public static _ReturnValue Add_LogView(T_LOGVIEW en)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                conn.T_LOGVIEW.Add(en);
                conn.SaveChanges();
                return _ReturnValue.OK;
            }
            catch { return _ReturnValue.Err; }
        }
    }
}
